﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infrastructure.ObjectModel;
using Microsoft.Xna.Framework;
using B10_Ex02_EladLebovitch_034695114.Screens;
using Infrastructure.Managers;

namespace B10_Ex02_EladLebovitch_034695114.ObjectModel
{
    public class GameFlow : GameService
    {
        public int CurrentLevel { get; set; }
        public bool IsTwoPlayers { get; set; }
        private MainGameScreen m_MainGameScreen;
        private ScreensMananger m_screensMananger;
        public int PlayerOneScore { get; set; }
        public int PlayerTwoScore { get; set; }
        
        public GameFlow(Game i_Game) : base(i_Game)
        {
            PlayerOneScore = 0;
            PlayerTwoScore = 0;
            m_screensMananger = new ScreensMananger(this.Game);
        }

        /// <summary>
        /// Resets the game to the starting point
        /// </summary>
        public void RestartGameScreens()
        {
            // Reset all screens
            m_screensMananger.Clear();

            // Check if the main game screen has been init
            if (m_MainGameScreen != null)
            {
                Game.Services.RemoveService(typeof(MainGameScreen));
            }

            // Set a new main game screen
            m_MainGameScreen = new MainGameScreen(this.Game);
            Game.Services.AddService(typeof(MainGameScreen), m_MainGameScreen);

            m_screensMananger.Push(m_MainGameScreen);
            m_screensMananger.Push(new LevelTransitionScreen(this.Game));
            m_screensMananger.Push(new MainMenuScreen(this.Game));

            // Used by all game components
            m_screensMananger.SetCurrentScreen(new WelcomeScreen(this.Game));
        }

        /// <summary>
        /// Resets the game to the starting point
        /// </summary>
        public void RestartGameScreensForMenu()
        {
            // Reset all screens
            m_screensMananger.Clear();

            // Check if the main game screen has been init
            if (m_MainGameScreen != null)
            {
                Game.Services.RemoveService(typeof(MainGameScreen));
            }

            // Set a new main game screen
            m_MainGameScreen = new MainGameScreen(this.Game);
            Game.Services.AddService(typeof(MainGameScreen), m_MainGameScreen);

            m_screensMananger.Push(m_MainGameScreen);
            m_screensMananger.Push(new LevelTransitionScreen(this.Game));

            // Used by all game components
            m_screensMananger.SetCurrentScreen(new MainMenuScreen(this.Game));
        }

        /// <summary>
        /// Resets the game to the starting point
        /// </summary>
        public void RestartGameScreensForGame()
        {
            // Reset all screens
            m_screensMananger.Clear();

            // Check if the main game screen has been init
            if (m_MainGameScreen != null)
            {
                Game.Services.RemoveService(typeof(MainGameScreen));
            }

            // Set a new main game screen
            m_MainGameScreen = new MainGameScreen(this.Game);
            Game.Services.AddService(typeof(MainGameScreen), m_MainGameScreen);

            m_screensMananger.Push(m_MainGameScreen);

            // Used by all game components
            m_screensMananger.SetCurrentScreen(new LevelTransitionScreen(this.Game));
        }

        public void NextLevel()
        {
            // Advance to next level
            this.CurrentLevel++;

            // Restart the main game screen
            Game.Services.RemoveService(typeof(MainGameScreen));
            m_MainGameScreen = new MainGameScreen(this.Game);
            Game.Services.AddService(typeof(MainGameScreen), m_MainGameScreen);

            // Set the next screen
            m_screensMananger.Push(m_MainGameScreen);

            // Set the new screen
            m_screensMananger.SetCurrentScreen(new LevelTransitionScreen(this.Game));
        }
    }
}